﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System.Windows.Forms;

namespace MockPrjTeam1.Pages.Forms
{
    public partial class ContactDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtFirstName.Focus();

                var id = Convert.ToString(Request.QueryString["id"]);

                if (!string.IsNullOrEmpty(id))
                {
                    var biz = new ContactBiz();
                    var results = biz.GetOne(Convert.ToInt32(id));
                    txtFirstName.Text = results.FirstName;
                    txtSurName.Text = results.SurName;
                    hdfContact.Value = results.ManageID.ToString();
                    if (results.ManageID != 0)
                    {
                        ContactBiz ContactBiz=new ContactBiz();
                        Contact contact = ContactBiz.GetOne(results.ManageID);
                            if(contact !=null)
                            {
                                txtManagerName1.Text=contact.ContactName;
                            }

                    }
                   
                    DrDContactType.Items.Insert(0, results.ContactType);
                    txtKnownAs.Text = results.KnownAs;
                    txtOfficePhone.Text = results.OfficePhone;
                    txtMobilePhone.Text = results.MobilePhone;
                    txtHomePhone.Text = results.STHomePhone;
                    txtEmailAddress.Text = results.EmailAddress;
                    DrDBestContactMethod.Items.Insert(0, results.BestContactMethod);
                    txtJobRole.Text = results.JobRole;
                    txtJobTitle.Text = results.JobTitle;
                    txtWorkBase.Text = results.WorkBase;
                    if (results.Active == "Yes") CkbIsActive.Checked = true;
                    else CkbIsActive.Checked = false;
                }
            }           
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/ViewSearchContact.aspx",false );
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var id = Convert.ToString(Request.QueryString["id"]);
            
            var contact = new Contact();

            if (!string.IsNullOrEmpty(id))
            {
                contact.ContactID = Convert.ToInt32(id);
            }
                        
            contact.FirstName = txtFirstName.Text;
            contact.SurName = txtSurName.Text;
            contact.ContactType = DrDContactType.SelectedValue;
            contact.ManageID = !string.IsNullOrEmpty(hdfContact.Value) ? Convert.ToInt32(hdfContact.Value) : 0;
            contact.KnownAs = txtKnownAs.Text;
            contact.OfficePhone = txtOfficePhone.Text;
            contact.MobilePhone = txtMobilePhone.Text;
            contact.STHomePhone = txtHomePhone.Text;
            contact.EmailAddress = txtEmailAddress.Text;
            contact.BestContactMethod = DrDBestContactMethod.SelectedValue;
            contact.JobRole = txtJobRole.Text;
            contact.JobTitle = txtJobTitle.Text;
            contact.WorkBase = txtWorkBase.Text;
            if (CkbIsActive.Checked == true) contact.Active = "Yes";
            else contact.Active = "No";
            contact.ContactName = txtFirstName.Text + "  " + txtSurName.Text;
            
            var biz = new ContactBiz();

            bool result;

            if (!string.IsNullOrEmpty(id))
                result = biz.EditContact(contact);
            else result = biz.Save(contact);

            if (result)
            {
                Response.Redirect("../Views/ViewSearchContact.aspx", false);
            }
        }
               
    } 
}